--登陆消息处理
module("NetWork.Action.LoginAction", package.seeall)

NetCmdActionBase = require("NetWork.Action").NetCmdActionBase
LoginAction = NetCmdActionBase:new()
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr

function LoginAction:Action(bytes)
	local GameScene = require("Scene.GameScene").GameScene
	if GameScene.swServerCrRoom then
		GameScene.swServerCrRoom = false
		self:SwServerCrJoinRoomLogin(bytes)
		return
	end

	if GameScene.swServerJoinRoom then
		GameScene.swServerJoinRoom = false
		self:SwServerCrJoinRoomLogin(bytes)
		return
	end

	--正常登陆
	if not GameScene.resLoaded then
		self:NormalLogin(bytes)
	else
		--退出登录
		if GameScene.isNeedNormalLogin then
			self:NormalLogin(bytes)
		else
			local JBCModule = require("Module.JBCModule").JBCModule
			local GameHost = JBCModule.GameHost
			if GameHost ~= nil and GameHost.PlayType ~= nil then
				--再游戏中
				self:FightLogin(bytes)
			else
				--再大厅中
				self:LobbyLogin(bytes)
			end
		end
	end
end

function LoginAction:LobbyLogin(bytes)
	local LoginSm = require("Protol.loginMessage_pb").LoginSm()
	LoginSm:ParseFromString(bytes)
	Debug.log(stringify(LoginSm))
	
	local state = LoginSm.code --0失败1成功2用户不存在3用户已经在线4服务器爆满
	Debug.log("登陆状态返回1：" .. state)
	if 0 == state then
		local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		local str = UIWinMgr:GetIntTextByCode(51)
		FightOfflineModule.FightOffLineNotice(str)
	elseif 1 == state then
		local MainUserData = require("DynamicData.MainUserData").MainUserData
		MainUserData:LoginUserData(LoginSm)
		UIWinMgr:CloseMask()
	else
		--local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
		--FightOfflineModule.FightOffLineNotice("重连失败")
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		UIWinMgr:OpenErrorNotice(state)
	end
end

function LoginAction:FightLogin(bytes)
	local LoginSm = require("Protol.loginMessage_pb").LoginSm()
	LoginSm:ParseFromString(bytes)
	Debug.log(stringify(LoginSm))
	
	local state = LoginSm.code --0失败1成功2用户不存在3用户已经在线4服务器爆满
	Debug.log("登陆状态返回2：" .. state)
	if 0 == state then
		local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		local str = UIWinMgr:GetIntTextByCode(51)
		FightOfflineModule.FightOffLineNotice(str)
	elseif 1 == state then
		local MainUserData = require("DynamicData.MainUserData").MainUserData
		MainUserData:LoginUserData(LoginSm)
		
		local offlineRoomID = LoginSm.roomId
		if tonumber(offlineRoomID) > 0 then
			local ClubModule = require("Module.ClubModule").ClubModule
			ClubModule.JoinRoomCm(tonumber(offlineRoomID))
		else
			--local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
			--FightOfflineModule.FightOffLineNotice("您已经不在房间中", true)
			local JBCModule = require("Module.JBCModule").JBCModule
			local GameHost = JBCModule.GameHost
			Debug.log("LoginAction:FightLogin")
			GameHost:BackToLobby()
		end
	else
		local FightOfflineModule = require("Module.FightOfflineModule").FightOfflineModule
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		local str = UIWinMgr:GetIntTextByCode(51)
		FightOfflineModule.FightOffLineNotice(str)
		--local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		UIWinMgr:OpenErrorNotice(state)
	end
end

function LoginAction:SwServerCrJoinRoomLogin(bytes)
	local LoginSm = require("Protol.loginMessage_pb").LoginSm()
	LoginSm:ParseFromString(bytes)
	local state = LoginSm.code --0失败1成功2用户不存在3用户已经在线4服务器爆满
	
	Debug.log("切服登陆状态返回7：" .. state)
	if 0 == state then
		Debug.log("登陆失败")
	elseif 1 == state then
		local MainUserData = require("DynamicData.MainUserData").MainUserData
		MainUserData:LoginUserData(LoginSm)

		local LoginModule = require("Module.LoginModule").LoginModule
		if LoginModule.swServerDel ~= nil then
			LoginModule.swServerDel()
			LoginModule.swServerDel = nil
		end
	else
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		UIWinMgr:OpenErrorNotice(state)
	end
end

function LoginAction:NormalLogin(bytes)
	local LoginSm = require("Protol.loginMessage_pb").LoginSm()
	LoginSm:ParseFromString(bytes)
	local state = LoginSm.code --0失败1成功2用户不存在3用户已经在线4服务器爆满
	
	Debug.log("登陆状态返回3：" .. state)
	if 0 == state then
		Debug.log("登陆大厅失败")
	elseif 1 == state then
		Debug.log("登陆大厅成功")
		loginWin = UIWinMgr:GetWindow("LoginWin")
		local MainUserData = require("DynamicData.MainUserData").MainUserData
		MainUserData:LoginUserData(LoginSm)
		
		local GameScene = require("Scene.GameScene").GameScene
		function GetMWRoomCallBack(roomID)
			if not GameScene.resLoaded then
				GameScene:LoadBattleRes()
			end
			local lastClubID = nil
			if UnityEngine.PlayerPrefs.HasKey("lastClubID") then
				lastClubID = UnityEngine.PlayerPrefs.GetInt("lastClubID")
			end
			local lastdatingID = nil
			if UnityEngine.PlayerPrefs.HasKey("lastdatingID") then
				lastdatingID = UnityEngine.PlayerPrefs.GetInt("lastdatingID")
			end
			if lastClubID ~= nil then
				Debug.log("LoginAction   lastClubID:" .. tonumber(lastClubID))
			end
			if lastdatingID ~= nil then
				Debug.log("LoginAction   lastdatingID:" .. tonumber(lastdatingID))
			end
			
			loginWin.LoadingMask.gameObject:SetActive(false)
			UIWinMgr:CloseWindow("LoginWin")
			GameScene.isNeedNormalLogin = false

			if lastClubID == nil and tonumber(lastClubID) ~= 0 and LoginSm.isCrossServer == 0 then
				if lastdatingID == nil and tonumber(lastdatingID) ~= 0 then
					local NewGameLoobyWin = UIWinMgr:OpenWindow("NewGameLoobyWin")
					NewGameLoobyWin:ToClubPage()
					--NewGameLoobyWin.ChoseItempaizhuo.value = true
				else
					local NewGameLoobyWin = UIWinMgr:OpenWindow("NewGameLoobyWin")
					--NewGameLoobyWin.ChoseItempaizhuo.value = true
					NewGameLoobyWin:ToClubPage()
				end

				UIWinMgr:OpenWindow("NewDianJiWin")
			else
				if LoginSm.isCrossServer == 1 then
					Event.Brocast(EventDefine.OnChangeSever) 
				elseif LoginSm.isCrossServer == 2 then
					Event.Brocast(EventDefine.OnChangeSever2) 
				end
			end
		end

		--通过判断房间号有没有 判断是不是在房间内
		local offlineRoomID = LoginSm.roomId
		if offlineRoomID ~= nil and tonumber(offlineRoomID) > 0 then
			if not GameScene.resLoaded then
				GameScene:LoadBattleRes()
			end

			if tonumber(offlineRoomID) > 0 then
				local ClubModule = require("Module.ClubModule").ClubModule
				--ClubModule.GetServerCm(0,tonumber(offlineRoomID))
				Debug.log("LoginSm.playType:______________"..LoginSm.playType)
				ClubModule.JoinRoomDel(tonumber(offlineRoomID), LoginSm.playType)
			end
		else
			local JBCModule = require("Module.JBCModule").JBCModule
			local GameHost = JBCModule.GameHost

			if GameHost then
				GameHost:BackToLobby()
			end
		end
		
		GetMWRoomCallBack("")
	else
		local UIWinMgr = require("UI.UIWinMgr").UIWinMgr
		UIWinMgr:OpenErrorNotice(state)
	end
	
	local LoginModule = require("Module.LoginModule").LoginModule
	LoginModule.Logining = false
end

require("NetWork.NetCmdSet").CmdSet[1001] = LoginAction --注册处理对象
